package com.mdx.controller;

import com.mdx.entity.User;
import com.mdx.service.UserService;
import com.mdx.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login-ui")
    public String loginUI(){
        return "/login";
    }

    @RequestMapping("/register-ui")
    public String registerUI(){
        return "/register";
    }

    @RequestMapping("/register")
    @ResponseBody
    public ResultVo register(User user){
        userService.register(user);
        return new ResultVo(0,"成功",null);
    }

    @RequestMapping("/login")
    @ResponseBody
    public ResultVo login(User user){
        Subject subject = SecurityUtils.getSubject();
        subject.login(new UsernamePasswordToken(user.getName(),user.getPassword()));
        return new ResultVo(0,"成功",null);
    }
}
